package com.fita.entity.principal;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fita.entity.authority.RoleTemplate;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;

/**
 * 
 * @author dan
 *
 */
@Entity
@Table(name = "principal")
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class Principal {
    
    @Id
    @GeneratedValue
    private Long id;
    
    private String username;
    
    @JsonIgnore
    private String password;
    
    private String authority;
    
    private String mobile;
    
    private String name;
    
    /**
     * 所属分区
     */
    @ManyToOne(fetch = FetchType.EAGER, optional = true)
    private District district;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public Principal() {
    }
    
    /**
     * @param username
     */
    public Principal(String username) {
        this.id = new Long(1);
        this.mobile = "13666123456";
        this.username = username;
        this.password = encodePassword(generateDefaultPassword());
        this.authority = "ROLE_ADMIN";
        this.name = "系统管理员";
        this.disabled = false;
    }
    
    /**
     * @return
     */
    private String generateDefaultPassword() {
        return StringUtils.substring(this.mobile, this.mobile.length() - 6);
    }
    
    /**
     * @param password
     * @return
     */
    public String encodePassword(String password) {
        String saltPassword = StringUtils.join(password, "{", this.mobile, "}");
        return DigestUtils.md5Hex(saltPassword);
    }
    
    /**
     * @return
     */
    public Principal resetPassword() {
        this.password = encodePassword(generateDefaultPassword());
        return this;
    }

    /**
     * @param password
     * @return
     */
    public Principal changePassword(String password) {
        this.password = encodePassword(password);
        return this;
    }

    /**
     * @return
     */
    public Principal initliaze(Principal principal,District district) {
        this.district = district;
        this.password = principal.encodePassword(generateDefaultPassword());
        this.disabled = false;
        return this;
    }
    
    /**
     * @param district
     * @return
     */
    public Principal evaluateDistrict(District district) {
        this.district = district;
        return this;
    }
    
    /**
     * @return
     */
    public Principal freeze() {
        this.disabled = true;
        return this;
    }
    
    /**
     * @return
     */
    public Principal activate() {
        this.disabled = false;
        return this;
    }

    /**
     * 是否是销售
     * @return
     */
    public boolean isSaler(){
        return  StringUtils.equalsAnyIgnoreCase(this.authority, RoleTemplate.SALE_DIRECTOR.toString(), RoleTemplate.SALE_USER.toString());
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @return the authority
     */
    public String getAuthority() {
        return authority;
    }

    /**
     * @return the mobile
     */
    public String getMobile() {
        return mobile;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the district
     */
    public District getDistrict() {
        return district;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @param authority the authority to set
     */
    public void setAuthority(String authority) {
        this.authority = authority;
    }

    /**
     * @param mobile the mobile to set
     */
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param district the district to set
     */
    public void setDistrict(District district) {
        this.district = district;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

}